package com.hdjk.view.callback;

import com.hdjk.view.entity.RiskTestReturnEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class SubmitRiskTestCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONObject object = (JSONObject) msg.getData();
        if (object!=null) {
            RiskTestReturnEntity entity = new RiskTestReturnEntity();
            if (!object.isNull("riskAppetite")) {
                entity.setRiskAppetite(object.getString("riskAppetite"));
            }
            if (!object.isNull("riskRating")) {
                entity.setRiskRating(object.getString("riskRating"));
            }
            if (!object.isNull("riskDescription")) {
                JSONObject des = object.getJSONObject("riskDescription");
                if (!des.isNull("point")) {
                    JSONObject option = des.getJSONObject("point");
                    Iterator<String> optKeys = option.keys();
                    StringBuffer point = new StringBuffer();
                    List<String> keys = new ArrayList<>();
                    while (optKeys.hasNext()) {
                        String optKey = optKeys.next();
                        keys.add(optKey);
                    }
                    Collections.sort(keys);
                    for (int j = 0; j < keys.size(); j++) {
                        if (!option.isNull(keys.get(j))) {
                            String m = option.getString(keys.get(j));
                            point.append(keys.get(j) + "、" + m + "。\n");
                        }
                    }
                    entity.setPoint(point.toString());
                }
                if (!des.isNull("title")) {
                    entity.setTitle(des.getString("title"));
                }
            }
            msg.setData(entity);
        }
        return msg;
    }
}
